package com.home.cop.employee.controller;

import com.home.cop.base.BaseController;
import com.home.cop.employee.model.SysRole;
import com.home.cop.employee.service.SysRoleService;
import com.home.cop.sysinfo.model.UserSession;
import com.home.cop.system.RequiredAuth;
import com.home.cop.system.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api("角色相关")
public class RoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;

    @RequiredAuth
    @PostMapping("/list")
    @ApiOperation(notes = "查询角色列表",value = "根据条件筛选角色列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "instName", value = "机构名字", required = false, dataType = "String", paramType = "query")
    })
    public Result list(@RequestParam(value = "instName",required = false) String instName){
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(instName)){
            params.put("instName",instName.trim());
        }
        return Result.returnSuccess().setData(roleService.list(params,getPageNum(),getPageSize()));
    }

    @RequiredAuth
    @PostMapping("/delete")
    @ApiOperation(notes = "删除角色",value = "根据角色Id删除角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result deleteRole(@RequestParam("roleId") Integer roleId){
        roleService.deleteRole(roleId);
        return Result.returnSuccess().setMessage("删除成功！");
    }

    @RequiredAuth
    @PostMapping("/add")
    @ApiOperation(notes = "新增角色",value = "新增角色")
    public Result addRole(@RequestBody SysRole role){
        roleService.insert(role);
        return Result.returnSuccess().setMessage("新增成功！");
    }

    @RequiredAuth
    @PostMapping("/updateRoleInfo")
    @ApiOperation(notes = "修改角色信息",value = "修改角色信息")
    public Result updateRoleInfo(@RequestBody SysRole role){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        roleService.updateRoleInfo(role);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @RequiredAuth
    @PostMapping("/updateRoleStatus")
    @ApiOperation(notes = "启用禁用角色",value = "启用禁用角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：0-启用，1-禁用", required = true, dataType = "Integer", paramType = "query")
    })
    public Result updateRoleInfo(@RequestParam(value = "roleId") Integer roleId,
                                 @RequestParam(value = "status") Integer status){
        UserSession userSession = getUserSession();
        if (userSession.getInstId() != 0 ){
            return Result.returnFail().setMessage("无权限！");
        }
        roleService.updateRoleStatus(roleId,status);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @RequiredAuth
    @PostMapping("/menus")
    @ApiOperation(notes = "角色菜单授权",value = "角色菜单授权")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "menus", value = "角色编号，多个用逗号隔开", required = false, dataType = "String", paramType = "query")
    })
    public Result updateRoleMenus(@RequestParam(value = "roleId") Integer roleId,
                                 @RequestParam(value = "menus") String menus){
        roleService.updateRoleMenus(roleId,menus);
        return Result.returnSuccess().setMessage("授权成功！");
    }

    @RequiredAuth
    @PostMapping("/countRoleUseNum")
    @ApiOperation(notes = "查看角色使用数量",value = "查看角色使用数量")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result countRoleUseNum(@RequestParam(value = "roleId") Integer roleId){
        return Result.returnSuccess().setData(roleService.countRoleUse(roleId));
    }

    @RequiredAuth
    @PostMapping("/getRoleMenus")
    @ApiOperation(notes = "获取当前角色下的菜单",value = "获取当前角色下的菜单（返回所有菜单方便授权，isChecked=true表示角色有的菜单）")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result getRoleMenus(@RequestParam(value = "roleId") Integer roleId){
        return Result.returnSuccess().setData(roleService.getRoleMenus(roleId));
    }

    @RequiredAuth
    @PostMapping("/getAllRoles")
    @ApiOperation(notes = "获取所有的角色",value = "获取所有的角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result getAllRoles(){
        return Result.returnSuccess().setData(roleService.getAllRoles());
    }
}
